import android.content.res.Resources
import android.util.TypedValue


/**
 * dp转px
 * @receiver Float
 * @return Float 转换后的px值
 */
fun Number.dp2px() = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    this.toFloat(),
    Resources.getSystem().displayMetrics
)

/**
 * dp转px
 * @receiver Float
 * @return Float 转换后的px值
 */
fun Number.sp2px() = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_SP,
    this.toFloat(),
    Resources.getSystem().displayMetrics
)

/**
 * px转dp
 */
val Number.dp: Int
    get() {
        val scale = Resources.getSystem().displayMetrics.density
        return (this.toInt() * scale + 0.5f).toInt()
    }

/**
 * px转dp，输出float
 */
val Number.dpF: Float
    get() {
        val scale = Resources.getSystem().displayMetrics.density
        return this.toFloat() * scale
    }